$(function(){
	
	document.documentElement.style.fontSize = window.innerWidth/3.75 + 'px';
	
	var giftId,userId,activityId;
	
	if(isIOS()){
		userId = getQueryStr('userId');
	}else{
		userId = JavascriptInterface.getUserId();
	}
	activityId = getQueryStr('activityId');
	
	$.ajax({
		type: "post",
		url: "/dingding-web/comActAction!checkIsShare.action",
		dataType: "jsonp",
		async: false,
		jsonp: "callback",
		jsonpCallback: "success_jsop",
		data: {
			userId: userId,
			activityId: activityId
		},
		success: function(data) {
			
			if(!data.responseBody) {
				//用户进入当前页面没有发起过分享,获取首页详情数据
				$.ajax({
					type: "post",
					url: "/dingding-web/comActAction!getShareIndexInfo.action",
					dataType: "jsonp",
					async: false,
					jsonp: "callback",
					jsonpCallback: "success_jsop",
					data: {
						userId: userId,
						activityId: activityId
					},
					success: function(data) {
						var tpl = document.getElementById('tpl1').innerHTML;
						var html = juicer(tpl,data.responseBody);
						$('body').html(html);
						
						//因为input[type='radio']在ios和android上显示的默认样式不同,在此对两种设备进行分别控制样式
						var ua = navigator.userAgent.toLowerCase();
						if(/iphone|ipad|ipod/.test(ua)) {
							$("input[type='radio']").css({'left':'21px','top':'-1px'});
						}
						else if(/android/.test(ua)) {
							$("input[type='radio']").css({'top':'-4px'});
						}
						
						
						$('.share-btn').on('click',function() {
							//如果所有礼品都剩0了，则点击分享按钮的时候提示用户此次活动已结束，请您关注下次活动
							//上线要改（这里只是两种礼品）
							if(data.responseBody.giftList[0].residualNumber >= data.responseBody.giftList[0].totalNumber && data.responseBody.giftList[1].residualNumber >= data.responseBody.giftList[1].totalNumber && data.responseBody.giftList[2].residualNumber >= data.responseBody.giftList[2].totalNumber){
								alert('非常感谢您参与此次活动，由于礼品有限，请您及时关注下次活动!');
							}
							else {
								//可以进行礼品选择
								giftId = $('.radio input:checked').attr('value');
								if(giftId == undefined) {
									//用户没有选择礼品提示用户选择礼品
									alert('请选择礼品');
								}
								else {
									//选择礼品之后提示用户是否确认选择该礼品，确认则调用分享方法，取消则用户可以重新选择
									var giftName;
									switch (parseInt(giftId)) {
										case 1:
											giftName = data.responseBody.giftList[0].giftName;
											break;
										case 2:
											giftName = data.responseBody.giftList[1].giftName;
											break;
										case 3:
											giftName = data.responseBody.giftList[2].giftName;
											break;
										default:
											break;
									};
									swal({
							            title:'',
							            text:'是否确认选择'+giftName+'礼品',
							            type: 'warning',
							            showCancelButton:"true",  
						                showConfirmButton:"true",  
						                confirmButtonText:"确认",  
						                cancelButtonText:"取消"
							       },function(){
										//调用startShare()方法
										startShare(userId, giftId, activityId);
										$("input[type='radio']").attr('disabled',true);
							       });
								}
							}
							
						});	
						
					}
				});
				
				
				
				
			}
			else {
				//用户进入当前页面已经发起过分享获取当前邀请详情
				
				$.ajax({
					type: "post",
					url: "/dingding-web/comActAction!getInviteInfo.action",
					dataType: "jsonp",
					async: false,
					jsonp: "callback",
					jsonpCallback: "success_jsop",
					data: {
						userId: userId,
						activityId: activityId
					},
					success: function(data) {
						var tpl = document.getElementById('tpl2').innerHTML;
						var html = juicer(tpl,data.responseBody);
						$('body').html(html);
						
						$('.share-btn').on('click',function (){
							//上线改
							giftId = data.responseBody.giftId;
							//调用startShare()方法
							startShare(userId, giftId, activityId);
							
						});
						
					}
				});
				
				
			}
		}
	});
	





	//发起分享
	function startShare(userId, giftId, activityId) {
		$.ajax({
			type: "post",
			url: "/dingding-web/comActAction!publishShare.action",
			dataType: "jsonp",
			async: false,
			jsonp: "callback",
			jsonpCallback: "success_jsop",
			data: {
				userId: userId,
				giftId: giftId,
				activityId: activityId
			},
			success: function(data) {
				
				
				if(isIOS()) {
					
					window.webkit.messageHandlers.takeActivityShare.postMessage({
						"title": data.responseBody.wechatTitle,
						"content": data.responseBody.wechatDesc,
						"url": data.responseBody.shareUrl
					});
					//1是分享给朋友 ，2是分享到朋友圈 
//					window.webkit.messageHandlers.activityShareByType.postMessage({
//						"type" : [1],
//						"title": "叮叮顺风车",
//						"content": data.responseBody.activityTitle,
//						"url": data.responseBody.activityUrl
//					});
				} else {
					JavascriptInterface.takeActivityShare(data.responseBody.wechatTitle, data.responseBody.wechatDesc, data.responseBody.shareUrl);
//					JavascriptInterface.activityShareByType([1],'叮叮顺风车', data.responseBody.activityTitle, data.responseBody.activityUrl);
				}
			}
		});
	}
})
